package com.ishare.api.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;

/**
 * @author 李嘉宾
 */
@Controller
@RequestMapping("/login")
public class ImageCodeController {

    /**
     * 获取验证码图片
     *
     * @param request  request
     * @param response res ponse
     * @throws IOException IOException
     */
    @RequestMapping("/getImageCode")
    public void getImageCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 100);
        captcha.setBackground(Color.WHITE);
        captcha.setTextAlpha(0.6F);
        request.getSession().setAttribute("IMAGE_CODE", captcha.getCode().toLowerCase());
        captcha.write(response.getOutputStream());
    }

}
